package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.login.LoginUserHolder;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kotlin.jvm.internal.Lambda;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;



    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;


    @Autowired
    private BrowsingHistoryService   browsingHistoryService;

//     根据公寓id分页查询房间列表
    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> iPage, Long id) {

      return   roomInfoMapper.pageItemByApartmentId(iPage , id);

    }

    //     根据id获取房间的详细信息
    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();


//        1、根据  房间 id 查询 当前id 对应的  公寓信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
//        roomDetailVo.setRoomNumber(roomInfo.getRoomNumber());
//        roomDetailVo.setId(roomInfo.getId());
//        roomDetailVo.setRent(roomInfo.getRent());
//        roomDetailVo.setApartmentId(roomInfo.getApartmentId());
//        roomDetailVo.setIsRelease(roomInfo.getIsRelease());

        BeanUtils.copyProperties(roomInfo , roomDetailVo);
        log.info("roomInfo:{}",roomInfo);

//        2、根据 房间号 对应的 公寓 去 查询 公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
//          2.1、根据 公寓的id 查询 公寓标签信息
        List<LabelInfo> labelInfo = apartmentInfoMapper.selectApartmentLabel(apartmentInfo.getId());
        log.info("label:{}", labelInfo);


//          2.2、查询 对应的 公寓图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoQueryWrapper.eq(GraphInfo::getItemId, roomInfo.getApartmentId());
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoQueryWrapper);
        if (!CollectionUtils.isEmpty(graphInfoList)) {
            List<GraphVo> graphVo = new ArrayList<>();
            for (GraphInfo graphInfo : graphInfoList) {
                GraphVo newGraphVo = new GraphVo();
                newGraphVo.setName(graphInfo.getName());
                newGraphVo.setUrl(graphInfo.getUrl());
                graphVo.add(newGraphVo);
            }
//            ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
            apartmentItemVo.setGraphVoList(graphVo);
            apartmentItemVo.setLabelInfoList(labelInfo);
            apartmentItemVo.setMinRent(roomInfo.getRent());
            roomDetailVo.setApartmentItemVo(apartmentItemVo);
        }

// =================以上都是 房间所属公寓信息
//        图片列表
//        查询  房间的 图片列表
        LambdaQueryWrapper<GraphInfo> roomGraphInfoQueryWrapper = new LambdaQueryWrapper<>();
        roomGraphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        roomGraphInfoQueryWrapper.eq(GraphInfo::getItemId, roomInfo.getId());
        List<GraphInfo> roomGraphInfoList = graphInfoMapper.selectList(roomGraphInfoQueryWrapper);
        if (!roomGraphInfoList.isEmpty()) {
            List<GraphVo> graphVoList = new ArrayList<>();
            for (GraphInfo graphInfo : roomGraphInfoList) {
                GraphVo graphVo = new GraphVo();
                BeanUtils.copyProperties(graphInfo, graphVo);
                graphVoList.add(graphVo);
            }
            roomDetailVo.setGraphVoList(graphVoList);
        }

//         属性信息列表
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectRoomAttrValue(roomInfo.getId());
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        log.info("attrValueVoList:{}", attrValueVoList);


//        配套信息列表

        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectFacilityInfo(roomInfo.getId());
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        log.info("facilityInfoList:{}", facilityInfoList);


//     根据  房间id 查询 标签 信息
        List<LabelInfo> labelInfoList = roomLabelMapper.selectLabelInfo(roomInfo.getId());
        roomDetailVo.setLabelInfoList(labelInfoList);
        log.info("labelInfoList:{}", labelInfoList);


//      查询当前房间的支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectRoomPaymentType(roomInfo.getId());
        roomDetailVo.setPaymentTypeList(paymentTypeList);

//    查询当前房间的  杂费列表
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.selectApartmentFeeValue(roomInfo.getApartmentId());

        roomDetailVo.setFeeValueVoList(feeValueVoList);


//        查询当前房间的租期列表
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectRoomLeaseTerm(roomInfo.getId());

        roomDetailVo.setLeaseTermList(leaseTermList);





//     保存 浏览历史
//        获取 当前 登录的  用户信息
        Long userId = LoginUserHolder.getLoginUser().getUserId();

//        根据 用户 id 和 房间id 保存 用户浏览记录
        // 这里比较的耗时，如果不采用异步的方式 ， 当 浏览记录全部存放在数据库中，才会显示浏览详情信息到前端页面
//        这里为了 提高 效率，将 使用 异步操作
//          使用 spring 提供的  @Async 注解进行实现
        browsingHistoryService.saveHistory(userId , id);
        log.info("房间的详情信息--->当前的线程:{}",Thread.currentThread().getName());

        //        4、优化，因为不会等到插入成功之后，才会响应给 客户端，所以就可以使用 异步的方式提高响应速度
//          可以使用 spring 提供 的  @Async 注解 解决

        return roomDetailVo;
    }


    @Override
    public Page<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {

        Page<RoomItemVo> roomItemVo = roomInfoMapper.pageItem(page, queryVo);

        return roomItemVo;
    }
}




